var global_ajajform_file_id;
var global_ajajform_file_id_dialog;
ajajform_htmltemplates.file_id={
		htmltemplate: '<div class="af-label"></div><div class="af-edit"><div class="af-html"></div><div class="af-buttons"><input type="button" class="af-change" value=""/><input type="button" class="af-remove" value=""/></div><div class="af-error"></div></div><div class="af-upload"></div><div style="clear: both;"></div>'
};

(function( $, undefined ) {
$.widget("ui.ajajform_file_id",$.ui.ajajform_widget, {
	/**
	 *	Class: ui.ajajform_file_id
	 *		Создает поле загрузки файла
	 * Properties:
	 *		options typeof Object 					- Настройки ajajform_file_id
	 *		options.allowUpload typeof boolean, default false	-
	 *		options.cssclass typeof String, default 'afw-fileid' 	- CSS класс добавляемый к DIV формы
	 *		options.value typeof String				- значение
	 *		options.label typeof String				- заголовок
	 *		options.previewSuffix typeof String, default '-preview1' - превью
	 *		options.width typeof integer, default null		- ширина блока
	 *		options.htmltemplate typeof HtmlString			- шаблон активный
	 *		options.htmltemplate_disableable  typeof HtmlString	- шаблон не активный 
	 *		options.validate typeof Function, default null		- функция валидации
	 *		options.change typeof Function, default null		- функция изменения состояния
	 *		options.noSelect typeof boolean, default false		-
	 *		options.noRemove typeof boolean, default false		-
	 *		options.htmltemplate_dialog typeof HtmlString		- шаблон чекбокса
	 *		options.dialogFileGroupId typeof Integer			- Насильно всегда file_group_id
	 *		options.defaultFileGroupId typeof Integer			- file_group_id если не задана
	 */
	options: {
		// \type boolean
		allowUpload: false,
		// \type cssclass
		cssclass: "afw-fileid",
		// \type object
		value: {},
		// \type text
		label: "",
		// \type string
		previewSuffix: "-preview1",
		// \type integer
		width: null,
		// \type html
		htmltemplate: null,// '<div class="af-label"></div><div class="af-edit"><div class="af-html"></div><div class="af-buttons"><input type="button" class="af-change" value=""/><input type="button" class="af-remove" value=""/></div><div class="af-error"></div></div><div class="af-upload"></div><div style="clear: both;"></div>',
		//htmltemplate_disableable: '<div class="af-label"></div><div class="af-disable"></div><div class="af-edit"><div class="af-html"></div><div class="af-buttons"><input type="button" class="af-change" value=""/><input type="button" class="af-remove" value=""/></div><div class="af-error"></div></div><div class="af-upload"></div><div style="clear: both;"></div>',
		// \type function
		validate: null,
		noSelect: false,
		noRemove: false,
		// \type: function
		change: null,
		htmltemplate_dialog: '<div><iframe src="" width="100%" height="95%" frameborder="0" border="0"></iframe></div>'
	},
	i18n: {
		en: {
			set: "Set",
			change: "Change",
			remove: "Remove"
		},
		ru: {
			set: "Выбрать",
			change: "Сменить",
			remove: "Очистить"
		}
	},
	_create: function() {
		this.genericCreate();
		this.i18n_texts();
		this.bindButtons();
		this.refreshLabel();
		this.refreshValue();
		//this.refreshDisabled();
		this.refreshRemoveButton();
		this.refreshAllowUpload();
		this.refreshNoRemove();
		this.refreshNoSelect();
	},
	i18n_texts: function() {
		$(".af-change",this.element).prop("value",this.gettext("change"));
		$(".af-remove",this.element).prop("value",this.gettext("remove"));
	},
	refreshRemoveButton: function() {
		this.refreshNoRemove();
		//if (!this.options.value.id) $(".af-remove").hide(); else $(".af-remove").show();
	},
	refreshNoRemove: function () {
		if (this.options.noRemove || !this.options.value.id) {
			$(".af-remove",this.element).hide();
		} else {
			$(".af-remove",this.element).show();
		}
	},
	refreshNoSelect: function () {
		if (this.options.noSelect) {
			$(".af-change, .af-name",this.element).hide();
		} else {
			$(".af-change, .af-name",this.element).show();
		}
	},



	fireChanged: function() {
		if (this.options.change) this.options.change(this.options.value);
	},
	refreshDialogFileGroupId: function() {
	},
	refreshDefaultFileGroupId: function() {
	},
	refreshValue: function() {
		var i=$(".af-html",this.element);
		var arr;
		if (this.options.value.url) {
			if ((arr=this.options.value.url.match(/^(.*)\.(png|gif|jpe?g)$/i))) {
				var preview=arr[1]+this.options.previewSuffix+"."+arr[2];
				i.html("<img src='"+preview+"'/><a class='af-small af-name' href='"+this.options.value.url+"'>"+this.options.value.name+"</a>");
			} else {
				i.html("<a href='"+this.options.value.url+"'>"+this.options.value.name+"</a>");
			}
		} else {
			i.html("");
		}
		this.refreshRemoveButton();
	},
	refreshAllowUpload: function() {
		var i=$(".af-upload",this.element);
		i.empty();
		if (this.options.allowUpload) {
			$('<input type="file" class="fileInput"/>').appendTo(i);
		}
	},
	bindButtons: function() {
		var t=this;
		$(".af-change",this.element).button().bind("click",{t:t},function(e) {e.data.t.openFileChooseDialog(e.data.t);});
		$(".af-remove",this.element).button().bind("click",{t:t},function(e) {e.data.t.removeValue();});
	},
	removeValue: function() {
		this.options.value={};
		this.refreshValue();
		this.refreshRemoveButton();
	},
	openFileChooseDialog: function(t) {
		var d=global_ajajform_file_id_dialog=$(this.options.htmltemplate_dialog).appendTo($("body"));
		var tail="";
		if (this.options.dialogFileGroupId) {
			tail="&file_folder_id="+this.options.dialogFileGroupId;
		} else if (this.options.value && this.options.value.id) {
			tail="&file_id="+this.options.value.id;
		} else if (this.options.defaultFileGroupId) {
			tail="&file_folder_id="+this.options.defaultFileGroupId;
		}
		$("iframe",d).attr("src","/admin_popup-AdminMediaLibrary/?target=ajajform_file_id"+tail);
		global_ajajform_file_id=t;
		var wx=Math.floor(win_x()*0.90)-100;
		var wy=Math.floor(win_y()*0.90)-100;
		d.dialog({
			modal: true,
			width: wx<830?830:wx,
			height: wy<550?470:wy,
			close: function() { d.dialog("destroy");d.remove(); }
		});
	},
	//refreshDisabled: function() {
	//	$("input",this.element).prop("disabled",this.options.disabled);
	//	this.genericRefreshDisabled();
	//},
	destroy: function() {
	},
	validate: function() {
		return false;
	},
	postvalue: function(r,r2) {
		r[this.options.name]=this.options.value.id||"";
		var v=$(".af-upload input",this.element).val();
		$(".af-upload input",this.element).prop("name",this.options.name+"-file");
		if (v && v.length) r2[this.options.name+"-file"]=v;
	}
});

})( jQuery );

function ajajform_file_id_set(v)
{
	global_ajajform_file_id._setOption("value",v);
	global_ajajform_file_id.fireChanged();
	global_ajajform_file_id=undefined;
	global_ajajform_file_id_dialog.dialog("close");
}

